package com.czu.service.impl;

import com.czu.service.LoginService;
import com.taotao.mapper.TbUserMapper;
import com.taotao.pojo.TbUser;
import com.taotao.pojo.TbUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class LoginServiceImpl implements LoginService {
    @Autowired
    private TbUserMapper mapper;

    @Override
    public String login(String username, String password) {
        try {
            if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
            {
                return "账号或者密码不能为空";
            }
            TbUserExample example = new TbUserExample();
            TbUserExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            criteria.andPasswordEqualTo(DigestUtils.md5DigestAsHex(password.getBytes()));
            List<TbUser> tbUsers = mapper.selectByExample(example);
            if(tbUsers==null||tbUsers.size()==0)
            {
                return "账号或者密码不正确";
            }
        }catch (Exception e) {
            return "登录失败";
        }
        return "登陆成功";
    }
}
